package com.s69.gyh.mybatis_plus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.s69.gyh.mybatis_plus.entity.Book;
import com.s69.gyh.mybatis_plus.mapper.BookMapper;
import com.s69.gyh.mybatis_plus.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Autowired
    BookMapper bookMapper;

    @Override
    public IPage getPage(Integer pageNum) {
        IPage page=new Page(pageNum,3);
        return bookMapper.selectPage(page, null);
    }

    @Override
    public List<Book> searchBook(Book book) {
//        QueryWrapper queryWrapper=new QueryWrapper<>();
//        if (book.getAuthor()!=null && book.getAuthor()!=""){
//            queryWrapper.like("author",book.getAuthor());
//        }
//        if (book.getName()!=null && book.getName()!=""){
//            queryWrapper.like("name",book.getName());
//        }
//        if (book.getCategory()!=null && book.getCategory()!=""){
//            queryWrapper.eq("category",book.getCategory());
//        }
//        return bookMapper.selectList(queryWrapper);
        LambdaQueryWrapper<Book> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (book.getAuthor() != null && book.getAuthor() != "") {
            lambdaQueryWrapper.like(Book::getAuthor, book.getAuthor());
        }
        if (book.getName() != null && book.getName() != "") {
            lambdaQueryWrapper.like(Book::getName, book.getName());
        }
        if (book.getCategory() != null && book.getCategory() != "") {
            lambdaQueryWrapper.eq(Book::getCategory, book.getCategory());
        }
        return bookMapper.selectList(lambdaQueryWrapper);
    }
}